//
//  TKIMKeFuMenuView.m
//  test2
//
//  Created by TryHone on 16/11/22.
//  Copyright © 2016年 TryHone. All rights reserved.
//

#import "TKIMKeFuMenuView.h"

@implementation TKIMKeFuMenuView

-(instancetype)init{
  
    self=[super init];
    if (self) {
        [self initUI];
    }
    return  self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat btnwidth=50;
    CGFloat width=self.frame.size.width;
    CGFloat height=self.frame.size.height;
    
    
    
        NSString* username=[TKIMHelper getShowNameWithUser:_user];
    
    
      NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18]};
      CGFloat length = [username boundingRectWithSize:CGSizeMake(20000, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
    [self.centerMenuBtn setTitle:username forState:UIControlStateNormal];
    
    [self.face sd_setImageWithURL:[NSURL URLWithString:_user.thumbPhotoUrl] placeholderImage:TKIMGetImage(@"im_icon_userDefault")];
    
    CGFloat offset=(length+40+10)/2;

    self.leftMenuBtn.frame=CGRectMake(0, 0, btnwidth, height);
    self.line1.frame=CGRectMake(btnwidth, 0, 1, height);
    
    self.face.frame=CGRectMake(self.centerX-offset, 2, 40, 40);
    self.centerMenuBtn.frame=CGRectMake(CGRectGetMaxX(self.face.frame)+10, 0, length, height);
    self.line2.frame=CGRectMake(width-btnwidth-1, 0, 1, height);
    
    self.rightMenuBtn.frame = CGRectMake(width-btnwidth, 0,btnwidth , height);
    self.reddot.frame=CGRectMake(5, 5, 10, 10);
    
    
}

-(void)initUI{
    [self addSubview:self.leftMenuBtn];
    [self addSubview:self.line1];
    [self addSubview:self.face];
    [self addSubview:self.centerMenuBtn];
    [self addSubview:self.line2];
    [self addSubview:self.rightMenuBtn];
    [self.leftMenuBtn.imageView addSubview:self.reddot];
    self.reddot.hidden=YES;
}

-(UIButton*)leftMenuBtn{

    if (_leftMenuBtn==nil) {
        _leftMenuBtn=[[UIButton alloc] init];
        _leftMenuBtn.backgroundColor=[UIColor clearColor];
        [_leftMenuBtn setImage:[UIImage imageNamed:@"kefu_menu"] forState:UIControlStateNormal];
        _leftMenuBtn.tag=106;
        [_leftMenuBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _leftMenuBtn;
}

-(UIButton*)centerMenuBtn{
    if (_centerMenuBtn==nil) {
        _centerMenuBtn=[[UIButton alloc] init];
        _centerMenuBtn.backgroundColor=[UIColor clearColor];
        _centerMenuBtn.titleLabel.font=[UIFont systemFontOfSize:18];
        [_centerMenuBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _centerMenuBtn;
}

-(UIButton*)rightMenuBtn{
 
    if (_rightMenuBtn==nil) {
        _rightMenuBtn=[[UIButton alloc] init];
        _rightMenuBtn.backgroundColor=[UIColor clearColor];
        [_rightMenuBtn setImage:TKIMGetImage(@"kefu_phone") forState:UIControlStateNormal];
        _rightMenuBtn.tag=105;
        [_rightMenuBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightMenuBtn;
}

-(UILabel*)reddot{

  
    if (_reddot==nil) {
        
        _reddot = [[UILabel alloc] init];
        _reddot.backgroundColor = [UIColor redColor];
        _reddot.layer.masksToBounds = YES;
        _reddot.layer.cornerRadius =5;
    }
    return _reddot;
}


-(UIView*)line1{

    if (_line1==nil) {
        
        _line1=[[UIView alloc] init];
        _line1.backgroundColor=[TKUIHelper colorWithHexString:@"#e6e6e6"];
    }
    return _line1;
}
-(UIView*)line2{
    
    if (_line2==nil) {
        
        _line2=[[UIView alloc] init];
        _line2.backgroundColor=[TKUIHelper colorWithHexString:@"#e6e6e6"];
    }
    return _line2;
}

-(void)setUser:(TKIMUser *)user{
    _user=user;
    [self setNeedsLayout];
    [self setNeedsDisplay];

}

-(UIImageView*)face{
    if (_face==nil) {
        _face=[[UIImageView alloc] init];
        _face.layer.masksToBounds=YES;
        _face.layer.cornerRadius=20;
    }
    return _face;
}

-(void)btnClick:(UIButton*)btn{

    if (btn.tag==105) {
     
        NSMutableDictionary* dict=_user.userExt;
        
        NSString* phone;
        if (dict) {
            phone=dict[@"phone"];
        }
        
        if (!(phone&&phone.length)) {
            phone=@"95358";
        }
        
        if (phone&&phone.length) {
//            NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];

        }else{
        
            [[TKIMLayerUtils instance] showTip:@"未获取到客服电话号码" hideAfterDelay:1];
            
        }
    }else if(btn.tag==106){//点击了返回按钮
        if (_block) {
            _block(nil,nil);
        }
    
    }
}


-(void)showReddot:(BOOL)flag{
    self.reddot.hidden=!flag;
}

@end
